package com.woniuxy.student.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.woniuxy.commons.entity.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/27 0027-上午 11:25
 */
public class StudentCommonUtil {
    /**
     * 给试卷绑定学生答案
     * @param testPaper
     * @return
     */
    public static TestPaper setValue(TestPaper testPaper, List<ReplySheet> sheetAnswers){
        List<String> answers = new ArrayList<>();
        try{
            if(sheetAnswers!=null){
                for (ReplySheet replySheet:sheetAnswers) {
                    answers.add(replySheet.getReplyAnswer());
                }
            }
            int count = 0;
            List<TestQuestion> questions = JSON.parseArray(JSON.toJSONString(testPaper.getPaperQuestions()),TestQuestion.class);
            System.out.println("类型:"+questions.getClass());
            for (TestQuestion testQuestion:questions) {
                int bankNum = (int)testQuestion.getPaperQuestionBank();
                switch (bankNum){
                    case 1:
                        SelectBank selectBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),SelectBank.class);
                        selectBank.setStudentAnswer(answers.get(count++));
                        System.out.println(selectBank.getStudentAnswer());
                        testQuestion.setPaperDetailQuestion(selectBank);
                        break;
                    case 2:
                        VacancyBank vacancyBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),VacancyBank.class);
                        vacancyBank.setStudentAnswer(answers.get(count++));
                        testQuestion.setPaperDetailQuestion(vacancyBank);
                        break;
                    case 3:
                        MaterialBank materialBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),MaterialBank.class);
                        List<SelectBank> selectBanks = JSON.parseArray(JSON.toJSONString(materialBank.getSelectBanks()),SelectBank.class);
                        List<VacancyBank> vacancyBanks = JSON.parseArray(JSON.toJSONString(materialBank.getVacancyBanks()),VacancyBank.class);
                        List<JudgeBank> judgeBanks = JSON.parseArray(JSON.toJSONString(materialBank.getJudgeBanks()),JudgeBank.class);
                        if(selectBanks!=null){
                            int j = 0;
                            for (SelectBank select:selectBanks) {
                                select = JSON.parseObject(JSON.toJSONString(select),SelectBank.class);
                                select.setStudentAnswer(answers.get(count++));
                                selectBanks.set(j++,select);
                            }
                        }
                        if(vacancyBanks!=null){
                            int j = 0;
                            for (VacancyBank vacancyBank1:vacancyBanks) {
                                vacancyBank1 = JSON.parseObject(JSON.toJSONString(vacancyBank1),VacancyBank.class);
                                vacancyBank1.setStudentAnswer(answers.get(count++));
                                vacancyBanks.set(j++,vacancyBank1);
                            }
                        }
                        if(judgeBanks!=null){
                            int j = 0;
                            for (JudgeBank judge:judgeBanks) {
                                judge = JSON.parseObject(JSON.toJSONString(judge),JudgeBank.class);
                                judge.setStudentAnswer(answers.get(count++));
                                judgeBanks.set(j++,judge);
                            }
                        }
                        materialBank.setSelectBanks(selectBanks);
                        materialBank.setJudgeBanks(judgeBanks);
                        materialBank.setVacancyBanks(vacancyBanks);
                        testQuestion.setPaperDetailQuestion(materialBank);
                        break;
                    case 4:
                        ClozeBank clozeBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),ClozeBank.class);
                        List<ClozeSelect> clozeSelects = JSON.parseArray(JSON.toJSONString(clozeBank.getClozeSelects()),ClozeSelect.class);
                        int j = 0;
                        for (ClozeSelect clozeSelect:clozeBank.getClozeSelects()) {
                            clozeSelect = JSON.parseObject(JSON.toJSONString(clozeSelect),ClozeSelect.class);
                            clozeSelect.setStudentAnswer(answers.get(count++));
                            clozeSelects.set(j++,clozeSelect);
                        }
                        clozeBank.setClozeSelects(clozeSelects);
                        testQuestion.setPaperDetailQuestion(clozeBank);
                        break;
                    case 6:
                        JudgeBank judgeBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),JudgeBank.class);
                        judgeBank.setStudentAnswer(answers.get(count++));
                        testQuestion.setPaperDetailQuestion(judgeBank);
                        break;
                    case 7:
                        WriterBank writerBank = JSON.parseObject(JSON.toJSONString(testQuestion.getPaperDetailQuestion()),WriterBank.class);
                        writerBank.setStudentAnswer(answers.get(count++));
                        System.out.println(writerBank.getStudentAnswer());
                        testQuestion.setPaperDetailQuestion(writerBank);
                        break;
                }
            }
            testPaper.setPaperQuestions(questions);
        }catch (Exception e){
            System.out.println(e);
        }
        System.out.println("test:"+testPaper.getPaperQuestions());
        return testPaper;
    }

    /**
     * 解析答案
     * @param replies
     * @return
     */
    public static List<ReplySheet> parseAnswersArray(String replies){
        List<ReplySheet> replySheets = new ArrayList<>();
        JSONArray array = JSONArray.parseArray(replies);
        for (int i = 0; i <array.size() ; i++) {
            ReplySheet replySheet = new ReplySheet();
            replySheet.setReplyAnswer((String) array.getJSONObject(i).get("sanswer"));
            replySheets.add(replySheet);
        }
        return replySheets;
    }

    /**
     * 返回考试过期时间的毫秒数
     * @param paper 试卷
     * @return
     * @throws ParseException
     */
    public static long getTime(TestPaper paper) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(paper.getPaperDate());
        return format.parse(paper.getPaperDate()).getTime()+Long.parseLong(paper.getPaperDuration())*1000*60;

    }

    /**
     *
     * @param startTime 考试开始的时间
     * @param testTime 考试时长
     * @return 1：考试还没开始；2：考试已结束；3：考试已开始；
     * @throws ParseException
     */
    public static int canStartTest(String startTime,String testTime) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = Long.parseLong(testTime); //考试时长
        long time1 = format.parse(startTime).getTime(); //开始的时间
        long time2 = new Date().getTime(); //当前时间
        if (time1>time2){
            return 1;
        }
        if (time2>=time1+time*1000*60){
            return 2;
        }
        return 3;
    }

    /**
     * 作业的key过期时间
     * @param testPaper
     * @return
     * @throws ParseException
     */
    public static long getHomeworkExpireTime(TestPaper testPaper) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long expireTime = format.parse(testPaper.getPaperExpireTime()).getTime();
        return expireTime-new Date().getTime();
    }

    public static double  getRange(int score,List<Integer> scores){
        Collections.sort(scores);
        Collections.reverse(scores);
        TreeSet<Integer> rankSet = new TreeSet<>();
        TreeSet<Integer> scoreSet = new TreeSet<>();
        List<Integer> ranklist = new ArrayList<>();
        List<Integer> scorelist = new ArrayList<>();
        int index = 0;
        int num = 0;
        double temp = -1;
        for (int i: scores) {
            index++;
            if (temp!=i){
                num = index;
            }
            temp = i;
            rankSet.add(num);
            scoreSet.add(i);
        }
        ranklist.addAll(rankSet);
        scorelist.addAll(scoreSet);
        System.out.println(ranklist);
        Collections.reverse(scorelist);
        System.out.println(scorelist);
        System.out.println("*******");
        for (int i = 0; i < ranklist.size(); i++) {
            if (score==scorelist.get(i)){
                double rankDouble = ranklist.get(i)*100/scores.size();
                return rankDouble;
            }
        }
        return -1;
    }
}
